package 力扣.动态规划.区间DP.背包问题;

import java.util.Scanner;

/**
 * 洛谷p1048 采药
 */
public class chaiyao {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int maxTime = scanner.nextInt();
        int nums = scanner.nextInt();
        int[] wights=new int[nums];
        int[] values=new int[nums];

        for (int i = 0; i <nums ; i++) {
            wights[i]=scanner.nextInt();
            values[i]=scanner.nextInt();
        }
        int maxValue = bagMax(maxTime, nums, wights, values);
        System.out.println(maxValue);

    }
    static int bagMax(int maxTime,int nums,int[] wights,int[] values){
        int[][] dp=new int[nums+1][maxTime+1];
        for (int i = 1; i <=nums ; i++) {
            int w=wights[i-1];
            int v=values[i-1];
            for (int j = 1; j <=maxTime ; j++) {
                if (j>=w){
                    dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-w]+v);
                }else {
                    dp[i][j]=dp[i-1][j];
                }
            }
        }
        return dp[nums][maxTime];
    }


}
